package gov.child.service.impl;

import gov.child.dao.ConsultDao;
import gov.child.entities.po.EConsult;
import gov.child.entities.vo.*;
import gov.child.service.ConsultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author fzh
 * @since 2017/9/14
 */
@Service("consultService")
public class ConsultServiceImpl implements ConsultService {

    @Autowired
    private ConsultDao consultDao;

    @Override
    public Consult findById(Integer id) {
        EConsult consult = consultDao.findById(id);
        if (Objects.isNull(consult)) return Consult.EMPTY_CONSULT;
        return Consult.parseFrom(consult);
    }

    @Override
    public List findByUser(User user) {
        return BeanTransformer.parseFrom(Consult.class, consultDao.findByUser(User.parseTo(user)));
    }

    @Override
    public boolean save(Consult consult) {
        EConsult con = Consult.parseTo(consult);
        consultDao.save(con);
        return true;
    }

    @Override
    public boolean update(Consult consult) {
        EConsult con = Consult.parseTo(consult);
        consultDao.update(con);
        return true;
    }

    @Override
    public boolean delete(Consult consult) {
        EConsult con = Consult.parseTo(consult);
        consultDao.delete(con);
        return true;
    }

    @Override
    public Page getPage(int pageSize) {
        long total = consultDao.total();
        return PageUtil.createPage(pageSize, total, 1);
    }
}
